var user_page = 1;

function fetch_users(page)
{	
	if(page == 1 && $('#next_button').hasClass('disabled'))
		return;
		
	if(page == -1 && $('#previous_button').hasClass('disabled'))
		return;

	if(page)
		user_page += page;
	
	if(user_page < 1)
		user_page = 1;
	else if(user_page == 1)
		$('#previous_button').addClass('disabled');
	else
		$('#previous_button').removeClass('disabled');
	
	$('.user_row').each(function() { $(this).remove(); });	//remove rows
	$('#table_loader').show();

	var checked;	
	$('input[name="search_by"]').each(function() { checked = $(this).is(':checked') ? $(this).val() : checked; });
	
	$.ajax({
		url: base_url + 'users/get/' + user_page + '/' + checked + '/' + $('input[name="search_value"]').val(),
		success: function(data){			
			
			if(data != '[]')
				$('#next_button').removeClass('disabled');
			else
				$('#next_button').addClass('disabled');
		
			$('#table_loader').hide();			
			var users = $.parseJSON(data);
			
			for(var i in users)
			{
				$('#user_table').append(
					'<tr id="user_' + users[i].id + '" class="user_row">' +
						'<td>' +
							'<a href="#">Roles</a> - <a href="javascript: delete_user(\'' + users[i].id + '\')">Delete</a>' +
						'</td>' +
						'<td class="col_username"' + ($('#col_username').is(':checked') ? '' : ' style="display:none;"') + '>' +
							 users[i].username + 
						'</td>' +
						'<td class="col_firstname"' + ($('#col_firstname').is(':checked') ? '' : ' style="display:none;"') + '>' +
							users[i].first_name +
						'</td>' +
						'<td class="col_lastname"' + ($('#col_lastname').is(':checked') ? '' : ' style="display:none;"') + '>' +
							users[i].last_name +
						'</td>' +
						'<td class="col_employee_id"' + ($('#col_employee_id').is(':checked') ? '' : ' style="display:none;"') + '>' +
							users[i].employee_id +
						'</td>' +
						'<td class="col_department"' + ($('#col_department').is(':checked') ? '' : ' style="display:none;"') + '>' +
							users[i].department_name +
						'</td>' +
					'</tr>'
				);
			}
		}
	});
}

var search_timeout;

function search_value_changed()
{	
	if(search_timeout)
		clearTimeout(search_timeout);

	search_timeout = setTimeout('fetch_users()', 1000);
}

function user_person_changed()
{
	$.ajax({
		url: base_url + 'persons/get/' + $('#person_list').val(),
		success: function(data){
			var person = $.parseJSON(data);
	
			$('#first_name').val(person.first_name);
			$('#last_name').val(person.last_name);
		}
	});
}

function create_user()
{
	$.ajax({
		url: base_url + 'users/create',
		data: $('#new_user_form').serialize(),
		type: 'POST', 
		success: function(data){
			fetch_users();
		}
	});
}

function delete_user(id)
{
	if(confirm('Are you sure you want to delete this user?'))
	{
		$.ajax({
			url: base_url + 'users/delete/' + id,
			success: function(data){
				$('#user_' + id).remove();
			}
		});
	}
}

function toggle_column(name)
{
	if($('#col_' + name).is(':checked'))
		$('.col_' + name).each(function() { $(this).show() });
	else
		$('.col_' + name).each(function() { $(this).hide() });
}